<?php

namespace backend\models;

use Yii;

/**
 * This is the model class for table "baseconfigs".
 *
 * @property string $CfgKey 配置键
 * @property string $CfgVal 配置值
 * @property string $CfgOptions 其他项
 * @property string $CfgDescribe 描述项
 */
class Baseconfigs extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'baseconfigs';
    }

    
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['CfgKey', 'CfgVal'], 'required'],
            [['CfgKey', 'CfgVal', 'CfgDescribe'], 'string', 'max' => 128],
            [['CfgOptions'], 'string', 'max' => 256],
            [['CfgKey'], 'unique'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'CfgKey' => '配置键',
            'CfgVal' => '配置值',
            'CfgOptions' => '其他项',
            'CfgDescribe' => '描述项',
        ];
    }

    //查询网站基本信息配置
    public static function getConfig()
    {
        $config['company'] = self::findOne(['CfgKey' => 'company']);
        $config['domain'] = self::findOne(['CfgKey' => 'domain']);
        $config['initialization'] = self::findOne(['CfgKey' => 'initialization']);
        $config['on'] = self::findOne(['CfgKey' => 'on-off']);
        $config['pagesize'] = self::findOne(['CfgKey' => 'pagesize']);
        $config['platform'] = self::findOne(['CfgKey' => 'platform']);
        return $config;
    }

    public function getModuleConfig($post, $page, $pageSize)
    {
        $where = 1;
        $offset = max(0, ($page-1)) * $pageSize;
        $sql = "select * from baseconfigs where $where limit $pageSize offset $offset";
        $configs = yii::$app->db->createCommand($sql)->queryAll();
        foreach ($configs as $key=>$val){
            $configs[$key]['status'] = '非管理员禁止操作';
        }
        $sqla = "select count(*) from baseconfigs where $where";
        $total = yii::$app->db->createCommand($sqla)->queryScalar();
        $result = ['statusCode'=>200, 'total'=>$total, 'page'=>1, 'message'=>'','limit'=>10,'data'=>$configs];
        return $result;
    }


    public function insertData($post)
    {
        $db = yii::$app->db;
        $transaction  = $db->beginTransaction();
        try {
            $config = new Baseconfigs();
            $config->CfgKey = $post['CfgKey'];
            $config->CfgVal = $post['CfgVal'];
            $config->CfgOptions = $post['CfgOptions'];
            $config->CfgDescribe = $post['CfgDescribe'];
            if ($config->save()) {
                return ['statusCode' => 200, 'message' => '参数配置提交成功'];
            }else{
                return ['statusCode' => 300, 'message' => '参数配置提交失败'];
            }
            $transaction->commit();
            return ['statusCode' => 200, 'message' => '参数配置提交成功'];
        }catch (\Exception $e){
            $transaction->rollBack();
            return ['statusCode' => 300, 'message' => '参数配置提交失败'];
        }
    }


    public function updateData($post)
    {
        $db = yii::$app->db;
        $transaction  = $db->beginTransaction();
        try {
            $config = self::findOne(['CfgKey' => $post['txt_ID']]);
            $config->CfgKey = $post['txt_ID'];
            $config->CfgVal = $post['CfgVal'];
            $config->CfgOptions = $post['CfgOptions'];
            $config->CfgDescribe = $post['CfgDescribe'];
            if ($config->save()) {
                return ['statusCode' => 200, 'message' => '参数配置修改成功'];
            }else{
                return ['statusCode' => 300, 'message' => '参数配置修改失败'];
            }
            $transaction->commit();
            return ['statusCode' => 200, 'message' => '参数配置修改成功'];
        }catch (\Exception $e){
            $transaction->rollBack();
            return ['statusCode' => 300, 'message' => '参数配置修改失败'];
        }
    }

    public function deleteData($post)
    {
        $cfgkey = explode(',', $post['ids']);
        $result = Baseconfigs::deleteAll(['in', 'CfgKey', $cfgkey]);
        if ($result){
            return ['statusCode' => 200, 'message' => '删除成功'];
        }
        return ['statusCode' => 300, 'message' => '删除失败'];
    }

}
